package com.rizzo.back.test.dao;

import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import java.util.List;

/**
 * TODO Insert JavaDoc!
 */
public class LuceneIndexRebuilder extends HibernateDaoSupport {

    private List<Class> classes;

    /**
     * TODO Method reindexEntities ...
     * @throws Exception when
     */
    public void reindexEntities() throws Exception {
        FullTextSession fullTextSession = Search.getFullTextSession(getSession());
        for (Class aClass : classes) {
            fullTextSession.purgeAll(aClass);
            List result = fullTextSession.createQuery("select entity from " + aClass.getSimpleName() + " entity").list();
            for (Object o : result) {
                fullTextSession.index(o);
            }
        }
        fullTextSession.flushToIndexes();
        fullTextSession.clear();
    }

    /**
     * Method setClasses sets the classes of this LuceneIndexRebuilder object
     *
     * @param classes of type List<Class>
     */
    public void setClasses(List<Class> classes) {
        this.classes = classes;
    }
    
}
